
#include "play.h"
#include<QMenu>
#include<QMenuBar>
#include<QPushButton>
#include<QLabel>
#include"coin.h"
#include"dataconfig.h"
#include<QPainter>
#include<QPropertyAnimation>

//play::play(QWidget *parent)
//    : QMainWindow{parent}
//{
 //   this->levelindex=level
//}
play::play(int level){
    //创建选关场景
    QString str=QString("你选择的第%1关").arg(level);
                  qDebug()<<str;
this->levelindex=level;
                  this->setFixedSize(320,588);
this->setWindowIcon(QIcon(":/po/res/ck.png"));
                  this->setWindowTitle("开翻！");
// 2. 创建菜单栏
QMenuBar* menubar =  menuBar();
setMenuBar(menubar);

// 3. 创建菜单栏开始
QMenu* startMenu = menubar->addMenu("开始");

// 4. 创建菜单项退出
QAction* quitAction = startMenu->addAction("退出");

// 5. 退出实现
connect(quitAction, &QAction::triggered, [=](){
    this->close();
});
QLabel *label=new QLabel;
label->setParent(this);
QFont font;
font.setFamily("微软雅黑");
    font.setPointSize(20);
QString str1=QString("Level: %1").arg(this->levelindex);
    label->setFont(font);
label->setText(str1);
label->setGeometry(30,this->height()-50,120,50);

//初始化每关数据
dataConfig config;//map类容器，用关卡做索引存入每关的二维数组
for(int i=0;i<4;i++){
    for(int j=0;j<4;j++){
        this->gk[i][j]=config.mData[this->levelindex][i][j];
    }
}
//胜利结算
QLabel* winLabel = new QLabel;
QPixmap tmpPix;
tmpPix.load(":/po/res/js.jpg");
winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
winLabel->setPixmap(tmpPix);
winLabel->setParent(this);
winLabel->move( (this->width() - tmpPix.width())*0.5 , -tmpPix.height());
//创建金币背景
for(int i = 0; i < 4; i++){
    for(int j = 0; j < 4; j++){
        QPixmap pix = QPixmap(":/po/res/BoardNode.png");
        QLabel* label = new QLabel;
        label->setGeometry(0,0,45,45);
        label->setPixmap(pix);
        label->setParent(this);
        label->move(59+i*50,205+j*50);
    //创建金币
        QString str;
        //显示金币或者银币
        if(this->gk[i][j]==1){
            str=":/po/res/Coin0001.png";

        }
        else{
            str=":/po/res/Coin0008.png";
        }
        coin *coin1=new coin(str);
        coin1->setParent(this);
//定义币的属性
        coin1->move(59+i*50,204+j*50);
        coin1->posx=i;
        coin1->posy=j;
        coin1->flag=this->gk[i][j];
//用二维数组操控每关的金币数据
        btn_coin[i][j]=coin1;
        //翻转金币
        connect(coin1,&coin::clicked,[=](){
            coin1->changeflag();

            // 反转周围的金币
            //上
            if(coin1->posx+1 >= 0){
                btn_coin[coin1->posx][coin1->posy-1]->changeflag();
                this->gk[coin1->posx][coin1->posy-1] = this->gk[coin1->posx][coin1->posy-1] == 0 ? 1 : 0;
            }

            //下
            if(coin1->posy+1 <= 3){
                btn_coin[coin1->posx][coin1->posy+1]->changeflag();
                this->gk[coin1->posx][coin1->posy+1] = this->gk[coin1->posx][coin1->posy+1] == 0 ? 1 : 0;
            }

            //左
            if(coin1->posx-1 >= 0){
                btn_coin[coin1->posx-1][coin1->posy]->changeflag();
                this->gk[coin1->posx-1][coin1->posy] = this->gk[coin1->posx-1][coin1->posy] == 0 ? 1 : 0;
            }

            //右
            if(coin1->posx+1 <= 3){
                btn_coin[coin1->posx+1][coin1->posy]->changeflag();
                this->gk[coin1->posx+1][coin1->posy] = this->gk[coin1->posx+1][coin1->posy] == 0 ? 1 : 0;
            }

            //判断是否胜利
            this->iswin=true;
            for(int i=0;i<4;i++){
                for(int j=0;j<4;j++){
                    if(btn_coin[i][j]->flag==false){
                        this->iswin=false;
                        break;
                    }
                }
            }
            if(this->iswin==true){
                qDebug()<<"游戏胜利！";
                QPropertyAnimation* ani = new QPropertyAnimation(winLabel, "geometry");
                ani->setDuration(1000);
                ani->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                ani->setEndValue(QRect(winLabel->x(),winLabel->y()+200,winLabel->width(),winLabel->height()));
                ani->setEasingCurve(QEasingCurve::OutBounce);
                ani->start();
            }

        });



    }
}
}

void play::paintEvent(QPaintEvent *){
// 1. 加载背景图片
QPainter painter(this);
QPixmap pix;
pix.load(":/po/res/caodi.jpg");
painter.drawPixmap(0,0,this->width(),this->height(),pix);
}
